package com.oocl.producer.impl;

import java.util.List;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

import com.oocl.connection.MQConnection;
import com.oocl.pojo.Book;
import com.oocl.producer.MsgProducer;
import com.oocl.util.FileIOUtil;
import com.oocl.util.JacksonUtil;

public class MsgProducerImpl implements MsgProducer {

	private Session session;
	private MessageProducer producer;

	public MsgProducerImpl() {

	}

	@Override
	public void onLine() {
		MQConnection.getInstance().start();

		this.session = MQConnection.getInstance().getSession();
		if (this.session == null)
			return;
		try {
			producer = this.session.createProducer(new ActiveMQQueue("jasper"));
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void sendMsg(String msg) {
		try {
			TextMessage message = this.session.createTextMessage(msg);
			producer.send(message);
			session.commit();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void offLine() {
		try {
			this.producer.close();// /是流，需要关
			this.session.close();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
